$().ready(function(){
    //获取分类 data-type 1 添加  2 修改
    var classList = [];
    function getClass(){
        getAjax(`${global.apiUrl}/get_community_service_shop_product_class`,res=>{
            // console.log(res);
            classList = res;
            if(res){
                var html = '';
                for(var i in res){
                    html += `
                    <div class="class-item themeBorder">
                        <div class="class-icon themeBgColor" data-toggle="modal" data-target="#classModal" data-id="${res[i].id}" data-type="2"><i class="fa fa-tags"></i></div>
                        <div class="class-text themeColor">${res[i].class_name}</div>
                        <div class="class-del themeBgColor" data-id="${res[i].id}">删除</div>
                    </div>
                `
                }
                html += `
                <div class="class-item themeBorder" id="add_btn" data-toggle="modal" data-target="#classModal" data-type="1">
                    <div class="class-icon"><i class="fa fa-plus" style="color:#0b67ff"></i></div>
                    <div class="class-text themeBgColor" style="color:#fff;">添加类别</div>
                    <div class="class-del"><i class="fa fa-plus themeColor" style="color:#0b67ff"></i></div>
                </div>
            `
                $('.community-shop-class').html(html)
            }
        },{
            shop_id:global.community_service_shop_id
        })
    }
    getClass();

    //modal show
    $('#classModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var type = btnThis.data('type');
        modal.find('#data_type').val(type);
        modal.find('#class_id').val('');
        modal.find('#class_name').val('');
        if(type==1){
            modal.find('#shopModalLabel').text('添加类别');
        }
        if(type==2){
            var class_id = btnThis.data('id');
            console.log(class_id);
            var class_name = getClassContent(class_id);
            modal.find('#shopModalLabel').text('修改类别');
            modal.find('#class_id').val(class_id);
            modal.find('#class_name').val(class_name);
        }

    });
    //根据class_id  获取class_name
    function getClassContent(id){
        for(var i in classList){
            if(id == classList[i].id){
                return classList[i].class_name;
            }
        }
    }
    //添加与修改
    $('#submit_class').on('click',function(){
        var class_name = $('#class_name').val();
        var type = $('#data_type').val();
        var pro = {};

        if(type == 1){
            pro = {
                class_name:class_name,
                shop_id:global.community_service_shop_id
            }
        }
        if(type == 2){
            var id = $('#class_id').val();
            pro = {
                id:id,
                class_name:class_name,
                shop_id:global.community_service_shop_id
            }
        }
        pro = JSON.stringify(pro);
        console.log(type, pro);
        getAjax(`${global.apiUrl}/Api/Admincommunityserviceshop/operation_community_service_shop_class`,res=>{
            console.log(res);
            if(res == 1){
                alert('提交成功！');
                $('#classModal').modal('hide');
                getClass();
            }
        },{
            type:type,
            pro:pro
        })
    });
    $('.community-shop-class').on('click','.class-del',function(){
        let id = $(this).data('id');
        let isConfirm = confirm('请确认删除该分类？');
        if(isConfirm){
            getAjax(`${global.apiUrl}/Api/Admincommunityserviceshop/operation_community_service_shop_class`,res=>{
                if(res==1){
                    alert('提交成功！');
                    getClass();
                }
                if(res == 2){
                    alert('该商品类别，下存在商品，无法删除');
                }
            },{
                type:3,
                id:id
            })
        }
    })


})
